package com.example.dllo.keepdemo.dynamic.latest;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.example.dllo.keepdemo.R;
import com.example.dllo.keepdemo.greendao.DynamicLatest;
import com.example.dllo.keepdemo.person.PersonMessageActivity;
import com.example.dllo.keepdemo.dynamic.bean.LatestBean;
import com.example.dllo.keepdemo.tools.CalculateTimeDifference;
import com.example.dllo.keepdemo.tools.RoundImageView;
import com.example.dllo.keepdemo.tools.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 16/1/13.
 */
public class LatestListViewAdapter extends BaseAdapter implements View.OnClickListener {
    private Context context;
    private List<LatestBean.DataEntity> list;
    private List<DynamicLatest> dynamicLatestList;
    private int test;

    public LatestListViewAdapter(Context context,List<LatestBean.DataEntity> list,int hasNet) {
        this.context = context;
        this.list = list;
        this.test = hasNet;
    }

    public LatestListViewAdapter(List<DynamicLatest> dynamicLatestList, Context context,int noneNet) {
        this.dynamicLatestList = dynamicLatestList;
        this.context = context;
        this.test = noneNet;
    }

    @Override
    public int getCount() {
        if (test == 1) {
            return dynamicLatestList.size();
        } else {
            return list.size();
        }
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder myViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_listview_fragment_latest,parent,false);
            myViewHolder = new MyViewHolder(convertView);
            convertView.setTag(myViewHolder);
        } else {
            myViewHolder = (MyViewHolder) convertView.getTag();
        }
        myViewHolder.imgHeader.setOnClickListener(this);

        if (test == 1) {
            myViewHolder.tvName.setText(dynamicLatestList.get(position).getName());
            myViewHolder.tvCity.setText(dynamicLatestList.get(position).getCity());
            myViewHolder.tvContent.setText(dynamicLatestList.get(position).getContent());
            myViewHolder.tvTime.setText(dynamicLatestList.get(position).getTime());
            myViewHolder.tvMetaName.setText("完成" + dynamicLatestList.get(position).getMetaName());
            myViewHolder.tvCount.setText("第" + dynamicLatestList.get(position).getCount() + "次");
            myViewHolder.tvPrais.setText(dynamicLatestList.get(position).getPraise() + "");
            myViewHolder.tvComment.setText(dynamicLatestList.get(position).getComment() + "");

            if (!(dynamicLatestList.get(position).getPicture().equals(""))) {
                myViewHolder.imgPicture.setVisibility(View.VISIBLE);
                ImageLoader.ImageListener listener = ImageLoader.getImageListener(myViewHolder.imgPicture,R.mipmap.placeholder41_41,R.mipmap.placeholder41_41);
                VolleySingleton.getImageLoader().get(dynamicLatestList.get(position).getPicture(),listener,360,300);
            } else {
                myViewHolder.imgPicture.setVisibility(View.GONE);
            }

            String headUrl = dynamicLatestList.get(position).getImgHead();
            if (headUrl.equals("") || !(headUrl.endsWith(".jpg"))) {
                myViewHolder.imgHeader.setImageResource(R.mipmap.person_45_45);
            } else {
                ImageLoader.ImageListener listener1 = ImageLoader.getImageListener(myViewHolder.imgHeader,R.mipmap.person_45_45,R.mipmap.person_45_45);
                VolleySingleton.getImageLoader().get(headUrl,listener1,80,80);
            }
        } else {
            myViewHolder.tvName.setText(list.get(position).getAuthor().getUsername());
            myViewHolder.tvCity.setText(list.get(position).getCity());
            myViewHolder.tvContent.setText(list.get(position).getContent());
            myViewHolder.tvTime.setText(CalculateTimeDifference.timeDifferenceUTC(list.get(position).getCreated()));
            myViewHolder.tvMetaName.setText("完成" + list.get(position).getMeta().getName());
            myViewHolder.tvCount.setText("第" + list.get(position).getMeta().getCount() + "次");
            myViewHolder.tvPrais.setText(list.get(position).getLikes() + "");
            myViewHolder.tvComment.setText(list.get(position).getComments() + "");

            if (!(list.get(position).getPhoto().equals(""))) {
                myViewHolder.imgPicture.setVisibility(View.VISIBLE);
                ImageLoader.ImageListener listener = ImageLoader.getImageListener(myViewHolder.imgPicture,R.mipmap.placeholder41_41,R.mipmap.placeholder41_41);
                VolleySingleton.getImageLoader().get(list.get(position).getPhoto(),listener,360,300);
            } else {
                myViewHolder.imgPicture.setVisibility(View.GONE);
            }

            String headUrl = list.get(position).getAuthor().getAvatar();
            if (headUrl.equals("") || !(headUrl.endsWith(".jpg"))) {
                myViewHolder.imgHeader.setImageResource(R.mipmap.person_45_45);
            } else {
                ImageLoader.ImageListener listener1 = ImageLoader.getImageListener(myViewHolder.imgHeader,R.mipmap.person_45_45,R.mipmap.person_45_45);
                VolleySingleton.getImageLoader().get(headUrl,listener1,80,80);
            }
        }


        return convertView;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_item_fragment_latest_headpicture :
                Intent intent = new Intent(context, PersonMessageActivity.class);
                context.startActivity(intent);
                break;
        }
    }

    class MyViewHolder {
        private RoundImageView imgHeader;
        private ImageView imgPicture;
        private TextView tvName,tvCity,tvTime,tvContent,tvMetaName,tvCount,tvPrais,tvComment;
        public MyViewHolder(View view) {
            imgHeader = (RoundImageView) view.findViewById(R.id.img_item_fragment_latest_headpicture);
            imgPicture = (ImageView) view.findViewById(R.id.img_item_fragment_latest_picture);
            tvName = (TextView) view.findViewById(R.id.tv_item_fragment_latest_name);
            tvCity = (TextView) view.findViewById(R.id.tv_item_fragment_latest_subtitle);
            tvContent = (TextView) view.findViewById(R.id.tv_item_fragment_latest_content);
            tvTime = (TextView) view.findViewById(R.id.tv_item_fragment_latest_time);
            tvMetaName = (TextView) view.findViewById(R.id.tv_item_fragment_latest_meta_name);
            tvCount = (TextView) view.findViewById(R.id.tv_item_fragment_latest_meta_count);
            tvPrais = (TextView) view.findViewById(R.id.tv_item_fragment_latest_praisenum);
            tvComment = (TextView) view.findViewById(R.id.tv_item_fragment_latest_replynum);
        }
    }
}
